import java.util.HashMap;
import java.util.Map;

public class Num13 {
    Map<Character, Integer> romanMap = new HashMap<Character, Integer>(){
        {
            put('I', 1);
            put('V', 5);
            put('X', 10);
            put('L', 50);
            put('C', 100);
            put('D', 500);
            put('M', 1000);
        }
    };

    public int romanToInt(String s) {
        if ("".equals(s) || s == null){
            return 0;
        }

        int res = 0;
        for (int i = s.length()-1; i >= 0; i--){
            int value = romanMap.get(s.charAt(i));
            res = i < s.length()-1 && value < romanMap.get(s.charAt(i+1)) ? res - value : res + value;
        }

        return res;
    }

    public static void main(String[] args) {
        Num13 num13 = new Num13();
        System.out.println(num13.romanToInt("MCMXCIV"));
    }
}
